﻿/**
 * nPnP - neither Perl not PHP
 *
 * Copyright (C) 2009 Christian Moeller
 *
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the 
 * Free Software Foundation; either version 3 of the License, or (at your option) 
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; 
 * if not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FParser.Parser.Expressions;

namespace FParser.Executor
{
    public static class BooleanOperation
    {
        /// <summary>
        /// Compares the left and the right side against the given Operator
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="op"></param>
        /// <returns></returns>
        public static bool Compare(string left, string right, EnumBooleanOperator op)
        {
            switch (op)
            {
                case EnumBooleanOperator.EQUAL:
                    return (cData(left, right) == 0);
                case EnumBooleanOperator.GREATER_EQUAL:
                    return (cData(left, right) >= 0);
                case EnumBooleanOperator.GREATER:
                    return (cData(left, right) > 0);
                case EnumBooleanOperator.SMALLER:
                    return (cData(left, right) < 0);
                case EnumBooleanOperator.SMALLER_EQUAL:
                    return (cData(left, right) <= 0);
                case EnumBooleanOperator.NOTEQUAL:
                    return (cData(left, right) != 0);
            }

            return false;
        }

        /// <summary>
        /// This has to be made more precise
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        private static int cData(string left, string right)
        {
            if (isNumber(left) && isNumber(right))
            {
                int iLeft = int.Parse(left),
                    iRight = int.Parse(right);

                return (iLeft.CompareTo(iRight));
            } 
            else
            {
                return (left.CompareTo(right));
            }
        }


        private static bool isBool(string data)
        {
            bool expression;
            return bool.TryParse(data, out expression);
        }

        /// <summary>
        /// Determines if the given data is an integer
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private static bool isNumber(string data)
        {
            int number;
            return int.TryParse(data, out number);
        }
    }
}
